<?php
	require_once($_SERVER["DOCUMENT_ROOT"]."/global.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/security_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/alerter/modules/watched_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/page_navigator.php");

	SecurityMod::login();

	$level = $_SESSION["account"]->getLevel();
	if ($level != "Admin" && $level != "AlertUser")
		SecurityMod::logout();
	
	$aid = $_SESSION["account"]->getName();
	$pageNumber = intval(v($_REQUEST, "page"));
	if ($pageNumber < 1)
		$pageNumber = 1;

	function drawNavigator() {
		global $pageCount, $pageNumber;
		$params = "";
		PageNavigator::draw($pageCount, $pageNumber, 33, $params, "watch_list.php");
	}

	$watched = WatchedMod::getWatchedPilots($pageNumber, $pageCount);

?>
<html>
<head>
<title><?php echo(SettingsMod::PAGE_TITLE." :: Watched"); ?></title>
<link rel="stylesheet" href="main.css">
<script type="text/javascript">
function validateForm(){
	var errMsg = "";
	var pid = document.forms["newEntry"]["pilot_id"].value;
	var name = document.forms["newEntry"]["pilot"].value;
	if(pid==null || pid==""){
		errMsg+="You must enter the pilot id\n";
	}
	if(name==null || name==""){
		errMsg+="You must enter the pilot name\n";
	}
	if(errMsg != ""){
		alert(errMsg);
		return false;
	}
}
</script>
</head>
<body>
	<h2 align="center">Watched Pilots List</h2>
	<table align="center" width="600">
	<tr>
		<td align="center">
			<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center" width="100%">
			<tr>
				<td colspan="7"><?php drawNavigator()?></td>
			</tr>
			<tr>
				<th>&nbsp;</th>
				<th><u>Name</u></th>
				<th><u>Id</u></th>
				<th><u>Faction</u></th>
				<th><u>Alliance</u></th>
				<th><u>Edit</u></th>
				<th><u>Delete</u></th>
			</tr>
			<?php
				$i = 0;
				foreach ($watched as $wtc):
				$i++;
			?>
			<tr bgcolor='#0b0b2f'>
				<!-- Row number -->
				<td align='right' style='cursor:crosshair'>
					<?php echo(($pageNumber - 1) * SettingsMod::PAGE_RECORDS_PER_PAGE + $i)?>.
				</td>
				<!-- Pilot name -->
				<td><a href="http://logger.fedfalcons.com/combats.php?universe=Orion&opponent=<?php echo($wtc->getName()); ?>"><?php echo($wtc->getName()); ?></a></td>
				<!-- Pilot Id -->
				<td><?php echo($wtc->getPid()); ?></td>
				<!-- Faction -->
				<td><?php echo($wtc->getFaction()); ?></td>
				<!-- Alliance-->
				<td><?php echo($wtc->getAlliance()); ?></td>
				<!-- Edit -->
				<form method="post" action="watch_list_edit.php">
					<td style="text-align: center">
						<input type="hidden" name="id" value="<?php echo($wtc->getId()); ?>" />
						<input type="submit" value="Edit" />
					</td>
				</form>
				<!-- Delete -->
				<form action="watch_list_delete.php" method="get">
				<td style="text-align: center">
						<input type="hidden" name="id" value="<?php echo($wtc->getId()); ?>" />
						<input type="hidden" name="name" value="<?php echo($wtc->getName()); ?>" />
						<input type="submit" value="Delete" />
				</td>
				</form>
			</tr>
			<?php endforeach; ?>
			<tr>
				<td colspan="99"><?php drawNavigator()?></td>
			</tr>
		</td>
	</tr>
	</table>

	<br />
	<h2 align="center">Add watch list entry</h2>
	<form name="newEntry" onsubmit="return validateForm()" method="post" action="watch_list_add.php">
	<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center">
	<tr>
		<th>Pilot ID:</th>
		<td><input type="text" name="pilot_id" style="width: 100%" /></td>
	</tr>
	<tr>
		<th>Pilot Name:</th>
		<td><input type="text" name="pilot" style="width: 100%" /></td>
	</tr>
	<tr>
		<th>Faction:</th>
		<td>
			<select name="faction" style="width: 100%" />
				<option value="Empire">Empire</option>
				<option value="Union">Union</option>
				<option value="Neutral">Neutral</option>
				<option value="Federation">Federation</option>
			</select>
		</td>
	</tr>
	<tr>
		<th>Alliance:</th>
		<td><input type="text" name="alliance" style="width: 100%" /></td>
	</tr>
	<td><input type="hidden" name="alerter" value="<?php echo($aid); ?>" style="width: 100%" /></td>
	<tr>
		<td colspan="2" align="center"><input type="submit" value="Create Entry" style="width: 100%" /></td>
	</tr>
	</table>
	</form>

</body>
</html>